<?php

namespace Biz\service\esSearch;

use Doraemon\tools\Filter;

class Utils
{

    // 字符转码,主要是为了在solr存储的时候字符编码为utf8，而表情的编码不是，所以就会有错误！
    public static function emojiStrConvert($emoji_str)
    {
        $str = $emoji_str;
        if ($str) {
            $str = Filter::filterEmoji($str);
        }
        return $str;
    }

    // 获取用户输入的点图标签的品牌和产品类型
    public static function extractBrandWithProductStr($image_tag_str = '')
    {
        $image_tags = json_decode($image_tag_str, true);
        $text = '';
        if ($image_tags && is_array($image_tags)) {
            foreach ($image_tags as $img_tag) {
                if (isset($img_tag['brand']) && $img_tag['brand']) {
                    $text .= (string)$img_tag['brand'] . ' ';
                }

                if (isset($img_tag['user_tag']) && $img_tag['user_tag']) {
                    $text .= (string)$img_tag['user_tag'] . ' ';
                }

                if (isset($img_tag['product_type']) && $img_tag['product_type']) {
                    $text .= (string)$img_tag['product_type'] . ' ';
                }

                // 商品型点图标签
                if (isset($img_tag['goods_info']) && is_array($img_tag['goods_info']) && $img_tag['goods_info']) {
                    $category_name = isset($img_tag['goods_info']['category_name']) && $img_tag['goods_info']['category_name'] ? trim($img_tag['goods_info']['category_name']) : "";
                    $goodsRemark = isset($img_tag['goods_info']['brand_name']) && $img_tag['goods_info']['brand_name'] ? trim($img_tag['goods_info']['brand_name']) : $category_name;
                    $text .= (string)$goodsRemark . " ";
                }

                // 新版本点图标签
                if (isset($img_tag["tag_name"]) && $img_tag["tag_name"]) {
                    $text .= (string)trim($img_tag["tag_name"]) . " ";
                }
            }
        }
        return trim($text);
    }
}